var DBN = {};
var BASE_DIR = "";
var ACTIVE_FLOAT = false;
var FLOAT_TIME = 1;
var FLOAT_TIME_DELAY = 0;
var FLOAT_OPACITY = 0.8;
var LAST_POS_X = 0;
var LAST_POS_Y = 0;
var FLOAT_TIMER = 1;
var FLOAT_HOLD = false;
var FLOAT_OVER = false;
var is_IE = (document.all) ? 1 : 0;

// Init Components
DBN.init_core = function () {
    DBN.active_navigator();
    DBN.active_float();
    DBN.scroll_navigator('navigator');
    return this;
};

DBN.active_navigator = function () {
    var is_valid = true;
    is_valid = DBN.check_exists("#nav", true);
    if (is_valid) {
        is_valid = DBN.check_exists(".mega_container");
    } else {
        return false;
    }
    if (!is_valid) {
        return false;
    }
    jQuery("#nav li").hover(function () {
        jQuery(this).find(".mega_container").fadeIn(100);
    }, function () {
        jQuery(this).find(".mega_container").fadeOut(100);
    });
    return this;
};

DBN.active_slider = function (sl_slider, sl_prev, sl_next, number_item, slide_item, auto) {
    var is_valid = true;
    is_valid = DBN.check_exists(sl_slider, true);
    if (is_valid) {
        is_valid = DBN.check_exists(sl_prev, true);
    } else {
        return false;
    }
    if (is_valid) {
        is_valid = DBN.check_exists(sl_next, true);
    } else {
        return false;
    }
    if (!is_valid) {
        return false;
    }
    jQuery(sl_slider).carouFredSel({
        items:number_item,
        scroll:{
            item:slide_item,
            easing:"quadratic",
            pauseOnHover:true
        },
        direction:"left",
        auto:auto,
        prev:sl_prev,
        next:sl_next,
        mousewheel:true,
        swipe:{
            onMouse:true,
            onTouch:true
        },
        align:"center"
    });
    return this;
};

DBN.active_float = function () {
    if (!ACTIVE_FLOAT) {
        return this;
    }
    var is_valid = true;
    is_valid = DBN.check_exists("#article_float");
    if (!is_valid) {
        var box_float = '<div id="article_float" class="article_float"><div class="inner">';
        box_float += '<div class="frame"><img /></div>';
        box_float += '<h3 class="title no_margin pd_10"></h3>';
        box_float += '<p class="short_desc no_margin pl_10 pr_10 pb_10"></p></div>';
        box_float += '</div></div>';
        jQuery("body").append(box_float);
        jQuery("#article_float").show();
    }
    var box_float = jQuery("#article_float");
    jQuery("*[item-type='float']").each(function () {
        var title = jQuery(this).attr("data-title");
        var desc = jQuery(this).attr("data-desc");
        var image = jQuery(this).attr("data-image");
        var self = jQuery(this);
        jQuery(box_float).css({"opacity":0, "zIndex":-1});
        jQuery(box_float).stop().stop().stop();
        jQuery(self).mouseover(function () {
            var width = jQuery(this).width().toString().replace("px", "");
            jQuery(box_float).css({"opacity":0, "zIndex":-1});
            jQuery(box_float).stop().stop().stop();
            FLOAT_HOLD = true;
            window.clearTimeout(FLOAT_TIMER);
            FLOAT_TIMER = window.setTimeout(function () {
                if (FLOAT_HOLD == true && FLOAT_OVER == false) {
                    jQuery(box_float).css({"opacity":0, "zIndex":-1});
                    jQuery(box_float).stop().stop().stop();
                    DBN.hide_float_immediate();
                }
            }, 8000);
            window.setTimeout(function () {
                var offset = jQuery(self).offset();
                offset.top = offset.top.toString().replace("px", "");
                offset.left = offset.left.toString().replace("px", "");
                var over_window = (offset.left - width);
                var box_title = jQuery(box_float).find(".title");
                var box_desc = jQuery(box_float).find(".short_desc");
                var box_image = jQuery(box_float).find(".frame");
                jQuery(box_title).text(title);
                jQuery(box_desc).text(desc);
                jQuery(box_image).find("img").attr("src", image);
                var box_height = (jQuery(box_title).height().toString().replace("px", "") * 1);
                box_height += (jQuery(box_desc).height().toString().replace("px", "") * 1);
                box_height += (jQuery(box_image).height().toString().replace("px", "") * 1);
                var slide_middle = (box_height / 2);
                var sigma_x = Math.abs(LAST_POS_X - offset.left);
                var sigma_y = Math.abs(LAST_POS_Y - (offset.top - slide_middle));
                if (sigma_x == 0 && sigma_y < 0) {
                    jQuery(box_float).css({"opacity":1, "zIndex":1});
                    return;
                }
                time_range = FLOAT_TIME;
                if (LAST_POS_Y != 0 || LAST_POS_X != 0) {
                    var total_pos = Math.floor(LAST_POS_X + LAST_POS_Y);
                    var des_pos = Math.floor(offset.left * 1 + (offset.top - slide_middle));
                    time_range = Math.abs(total_pos - des_pos);
                }
                if (time_range < FLOAT_TIME) {
                    time_range = FLOAT_TIME;
                } else if (time_range >= 800) {
                    time_range = 800;
                }
                LAST_POS_Y = (offset.top - slide_middle);
                LAST_POS_X = offset.left;
                var ml = 0;
                if (over_window > 0) {
                    LAST_POS_X -= width;
                } else {
                    LAST_POS_X += width;
                    if (is_IE) {
                        LAST_POS_X = width;
                        ml += width / 2;
                    } else {
                        ml = +width;
                    }
                }
                jQuery(box_float).css({"zIndex":1});
                jQuery(box_float).animate({
                    "max-height":"100%",
                    "overflow":"auto",
                    "opacity":FLOAT_OPACITY,
                    "top":LAST_POS_Y,
                    "left":LAST_POS_X,
                    "margin-left":ml
                }, time_range, function () {
                    jQuery(box_float).stop().stop().stop();
                    jQuery(box_float).animate({
                        "opacity":1
                    }, time_range / 2, function () {
                        jQuery(box_float).stop().stop().stop();
                    });
                });
            }, FLOAT_TIME_DELAY);
        });
        jQuery("#article_float").mouseover(function () {
            FLOAT_HOLD = true;
            FLOAT_OVER = true;
        });
        jQuery(self).mouseout(function () {
            FLOAT_TIMER = window.setTimeout(function () {
                if (FLOAT_HOLD == true && FLOAT_OVER == false) {
                    jQuery(box_float).css({"opacity":0, "zIndex":-1});
                    jQuery(box_float).stop().stop().stop();
                    DBN.hide_float_immediate();
                } else {
                    FLOAT_OVER = false;
                    jQuery(box_float).css({"opacity":0, "zIndex":-1});
                    jQuery(box_float).stop().stop().stop();
                    DBN.hide_float_immediate();
                }
            }, 1000);
        });
    });
};

DBN.hide_float = function () {
    var box_float = jQuery("#article_float");
    if (jQuery(box_float).size() == 0) {
        return false;
    }
    FLOAT_TIMER = window.setTimeout(function () {
        jQuery(box_float).css({"opacity":0, "zIndex":-1});
        jQuery(box_float).stop().stop().stop();
    }, 0);
    return this;
};

DBN.hide_float_immediate = function () {
    var box_float = jQuery("#article_float");
    if (jQuery(box_float).size() == 0) {
        return false;
    }
    jQuery(box_float).css({"opacity":0, "zIndex":-1});
    jQuery(box_float).stop().stop().stop();
};
DBN.check_exists = function (selector, log) {
    var elements = jQuery(selector);
    if (jQuery(elements).size() == 0) {
        if (log) {
            console.log('Not Found Element With Query: ' + selector);
        }
        return false;
    }
    return true;
};

DBN.scroll_navigator = function (nav_id) {
    var menu = jQuery('#' + nav_id), pos = menu.offset();
    jQuery(window).scroll(function () {
        if (jQuery(this).scrollTop() > pos.top + menu.height()) {
            menu.hide(0, function () {
                jQuery(this).addClass('fixed').show();
            });
        }
        else if (jQuery(this).scrollTop() <= pos.top && menu.hasClass('fixed')) {
            menu.show(0, function () {
                jQuery(this).removeClass('fixed').show();
            });
        }
    });
    return this;
}
jQuery(function () {
    DBN.init_core();
});
